//
//  ZZTaskDataUpdateVC.swift
//  QMXK
//
//  Created by jenkin on 2018/12/13.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit

class ZZTaskDataUpdateVC: ZZBaseVC {

    var dataDict:Dictionary<String, Any>!
    var dataSaveBlock:DataSaveBlock?
    var dataArray:Array<Any>?
    var dataIndex:Int?
    let openTextField:UITextField! = UITextField.init()
    lazy var finishField:UITextField! = {
        return UITextField.init()
    }()
    lazy var minField:UITextField! = {
        return UITextField.init()
    }()
    lazy var maxField:UITextField! = {
        return UITextField.init()
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUI()
    }
    
    override func setUI() {
        super.setUI()
        
        let array:Array<UITextField>! = [self.openTextField, self.finishField, self.minField, self.maxField]
        let labelStrArray:Array<String>! = ["open", "finish", "min", "max"]
        
        
        self.dataArray = self.dataDict["data"] as! Array
        let data:Dictionary<String, Any> = self.dataArray?[self.dataIndex!] as! Dictionary
        let  textArray:NSArray = [ZZBaseUtil.shareUtil.getStrWithFloat(text: data["open"] as! CGFloat), ZZBaseUtil.shareUtil.getStrWithFloat(text: data["finish"] as! CGFloat) , ZZBaseUtil.shareUtil.getStrWithFloat(text: data["min"] as! CGFloat) , ZZBaseUtil.shareUtil.getStrWithFloat(text: data["max"] as! CGFloat)]
        
        for index in 0..<4{
            let labeStr:String! = labelStrArray[index]
            let label:UILabel! = ZZViewUtil.shareViewUtil.createLabel(textAlign: NSTextAlignment.center, text: labeStr, font: CommenConst.shareCommentConst.KNFONT(17), color: ColorConst.shareColorConst.titleColor(), autoLine: true)
            self.view.addSubview(label)
            let textField:UITextField = array[index]
            textField.keyboardType = UIKeyboardType.numbersAndPunctuation
            self.view.addSubview(textField)
            textField.backgroundColor = UIColor.white
            
            let text:String = textArray[index] as! String
            textField.text = text
            
            label.snp.makeConstraints { (make) in
                make.left.equalTo(30)
                make.height.equalTo(30)
                make.width.equalTo(100)
                make.top.equalTo(index * 50 + 100)
            }
            
            textField.snp.makeConstraints { (make) in
                make.left.equalTo(label.snp.right).offset(10)
                make.height.equalTo(30)
                make.centerY.equalTo(label)
                make.width.equalTo(100)
            }
        }
        
        let sureBtn:UIButton = UIButton.init()
        sureBtn.addTarget(self, action: #selector(sureAction(sender:)), for: UIControl.Event.touchUpInside)
        sureBtn.setTitle("确定", for: UIControl.State.normal)
        sureBtn.backgroundColor = ColorConst.shareColorConst.mainTintColor()
        self.view.addSubview(sureBtn)
        sureBtn.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.width.equalTo(100)
            make.height.equalTo(30)
            make.top.equalTo(self.view.snp.bottom).offset(-50)
        }
    }

    
    @objc func sureAction(sender:UIButton){
        var data:Dictionary<String, Any>? = Dictionary.init()
        data?["open"] = ZZBaseUtil.shareUtil.getFloatWihDatStr(text: self.openTextField.text!)
        data?["finish"] = ZZBaseUtil.shareUtil.getFloatWihDatStr(text:self.finishField.text!)
        data?["min"] = ZZBaseUtil.shareUtil.getFloatWihDatStr(text: self.minField.text!)
        data?["max"] = ZZBaseUtil.shareUtil.getFloatWihDatStr(text: self.maxField.text!)
        self.dataArray![self.dataIndex!] = data
        self.dataDict["data"] = self.dataArray
        ZZBaseUtil.shareUtil.saveTypeName(data: self.dataDict!)
        if self.dataSaveBlock != nil{
            self.dataSaveBlock!(self.dataDict!)
        }
        
        let action:UIAlertController = UIAlertController.init(title: "修改成功", message: "", preferredStyle: UIAlertController.Style.alert)
        let sure:UIAlertAction = UIAlertAction.init(title: "确定", style: UIAlertAction.Style.default, handler: nil)
        action.addAction(sure)
        self.present(action, animated: true, completion: nil)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
}
